﻿Option Strict On
Option Infer On
Option Explicit On

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.StoredProcModel
Imports GenDotNet.Task.Common
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">

Public Class ChildData
   Inherits BaseData
   Implements IChildData


   Private mChildKeys As IEnumerable(Of IRelationKeyData)
   Private mParentEntity As IEntityData

   Public Shared Function Load( _
            ByVal rootdata As RootData, _
            ByVal parentEntity As IEntityData, _
            ByVal recordSets As IEnumerable(Of XElement)) _
         As IEnumerable(Of IChildData)
      Dim list = New List(Of IChildData)
      For Each e In recordSets
         list.Add(New ChildData(rootdata, parentEntity, e))
      Next
      Return list
   End Function

   Private Sub New( _
            ByVal rootdata As RootData, _
            ByVal parentEntity As IEntityData, _
            ByVal element As XElement)
      MyBase.New(rootdata, element)
      mChildKeys = RelationKeyData.Load(rootdata, From x In element...<orm:ChildKey>)
      mParentEntity = parentEntity
   End Sub

   Public ReadOnly Property ChildEntity() As IEntityData _
            Implements IChildData.ChildEntity
      Get
         Return RootData.GetEntity(Element.@ChildSPSet)
      End Get
   End Property

   Public ReadOnly Property UniqueName() _
            As String _
            Implements IChildData.UniqueName
      Get
         Return Element.@TempChildTableName
      End Get
   End Property

   Public ReadOnly Property ChildKeys() _
            As IEnumerable(Of IRelationKeyData) _
            Implements IChildData.ChildKeys
      Get
         Return mChildKeys
      End Get
   End Property

   Public ReadOnly Property ParentEntity() _
            As IEntityData _
            Implements IChildData.ParentEntity
      Get
         Return mParentEntity
      End Get
   End Property
End Class
